package IteratorAndComposite.entry;

import IteratorAndComposite.IteratorInterfaces.Iterator;
import IteratorAndComposite.IteratorInterfaces.Menu;
import IteratorAndComposite.IteratorInterfacesImpl.DinerMenuJavaIterator;

/**
 * @Author: pengzx
 * @Description:
 * @Date: 14:46 2017/9/19
 * @Modified by: 1.0
 */
public class DinerMenu implements Menu {
    static final int MAX_ITEM = 6;
    int numberOfItems = 0;
    MenuItem[] menuItems;

    public DinerMenu()
    {
        menuItems = new MenuItem[MAX_ITEM];
        addItem("鸡公饭","鸡公加饭",false,10.5);
        addItem("土豆饭","土豆加饭",true,3.5);
        addItem("白切鸡饭","鸡公加饭",false,9.5);
        addItem("排骨饭","排骨加饭",false,11.99);
    }

    public void addItem(String name, String destription, boolean vegetarian, double price)
    {
        MenuItem menuItem = new MenuItem(name,destription,vegetarian,price);
        if (numberOfItems>=MAX_ITEM){
            System.out.println("不能添加菜单了...");
        }else{
            menuItems[numberOfItems] = menuItem;
            numberOfItems++;
        }
    }

    @Deprecated
    public MenuItem[] getMenuItems()
    {
        return menuItems;
    }


    //其他方法
    @Override
    @Deprecated
    public java.util.Iterator createIterator(){
        return new DinerMenuJavaIterator(menuItems);
    }

    public Iterator iterator(){
        return new itr();
    }

    private class itr implements Iterator{
        int p = 0;
        @Override
        public boolean hasNext()
        {
            if (menuItems.length<=p||menuItems[p] == null){
                return false;
            }else{
                return true;
            }
        }

        @Override
        public Object next()
        {
            MenuItem menuItem = menuItems[p];
            p++;
            return menuItem;
        }
    }


}